package whu.o2o;



import whu.o2o.util.ConstantData;

import whu.o2o.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ViewFlipper;

public class TweenActivity extends CommonActivity implements OnGestureListener, OnDoubleTapListener {
	ViewFlipper mViewFlipper;
	GestureDetector mGestureDetector;
	int i = 1;
	private final int COUNT = 4; // ��ͼ����

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tween);
		initialize();
		initialView();
	}

	private void initialize() {
		mGestureDetector = new GestureDetector(this);
	}

	public void initialView() {
		mViewFlipper = (ViewFlipper) findViewById(R.id.details);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	public boolean onDown(MotionEvent e) {
		return false;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (e1.getX() > e2.getX()) {
			i += 1;
			if (i > COUNT) {
				i = COUNT;
				gotoMain();
			} else {
				mViewFlipper.showNext();
			}
		} else if (e1.getX() < e2.getX()) {
			i -= 1;
			if (i < 1) {
				i = 1;
			} else {
				mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);
				mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);
				mViewFlipper.showPrevious();
				mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);
				mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);
			}
		} else {
			return false;
		}
		return true;
	}

	public void onLongPress(MotionEvent e) {
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	public void onShowPress(MotionEvent e) {
	}

	public boolean onSingleTapUp(MotionEvent e) {
		i += 1;
		if (i > COUNT) {
			i = COUNT;
			gotoMain();
		} else {
			mViewFlipper.showNext();
		}
		return true;
	}

	public boolean onDoubleTap(MotionEvent e) {
		return false;
	}

	public boolean onDoubleTapEvent(MotionEvent e) {
		return false;
	}

	public boolean onSingleTapConfirmed(MotionEvent e) {
		return false;
	}

	private void gotoMain() {
		mApplication.getSharedPreferences().edit().putBoolean(ConstantData.SharedPrefs.SPLASH, false).commit();

		Intent intent = new Intent(TweenActivity.this, MainActivity.class);
		startActivity(intent);
		finish();
	}
}